package com.orangelife.service.propFeeAccount.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeAccount.IPropFeeAccountService;
import com.orangelife.service.propFeeAccount.model.AmountHandleDto;
import com.orangelife.service.propFeeAccount.model.PropFeeAccountDto;
import com.orangelife.service.propFeeAmountLog.IPropFeeAmountLogService;
import com.orangelife.service.propFeeAmountLog.model.PropFeeAmountLogDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.IdUtil;
import com.orangelife.utils.PropertyUtils;
import com.orangelife.utils.StringUtils;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_13006015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeAccount类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-06-23 17:31:23
 * ==============================================================
 */
@Service("propFeeAccountService")
public class PropFeeAccountServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeAccountService {
	private static Logger logger = Logger
			.getLogger(PropFeeAccountServiceImpl.class);
	@Resource(name = "propFeeAmountLogService")
	private IPropFeeAmountLogService propFeeAmountLogService;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeAccountServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public synchronized int addPropFeeAccount(PropFeeAccountDto propFeeAccount)
			throws AppBizException {

		Assert.notNull(propFeeAccount);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeAccount.getStatus())) {
			if (propFeeAccount.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "状态长度超出");
			}
		}
		if (StringUtils.isBlank(propFeeAccount.getAccountID())) {
			propFeeAccount.setAccountID(IdUtil.getuuId());

		}
		List<PropFeeAccountDto> propFeeAccountDtos = this
				.findPropFeeAccountByRelevantID(null,
						propFeeAccount.getApartmentID(), "1",propFeeAccount.getAccountTypeCode());
		if (propFeeAccountDtos != null && propFeeAccountDtos.size() > 0) {
			throw new AppBizException(CodeConst.SYS_CODE_1002, "账号已经存在");

		}
		propFeeAccount.setItemStat(TypeConst.FLAG_ON);
		propFeeAccount.setTscreate(new Date());
		propFeeAccount.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeAccountMapper.insertSelective", propFeeAccount);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeAccount(PropFeeAccountDto propFeeAccount)
			throws AppBizException {

		Assert.notNull(propFeeAccount);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeAccount.getAccountID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeAccountDto oldPropFeeAccount = this
				.findPropFeeAccountByID(propFeeAccount.getAccountID());

		if (StringUtils.isBlank(propFeeAccount.getOperation())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "亲，操作类型");
		}

		if (!propFeeAccount.getOperation().equals("1")
				&& !propFeeAccount.getOperation().equals("2")) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "亲，操作类型错误");
		}

		if (!StringUtils.isBlank(propFeeAccount.getStatus())) {
			if (propFeeAccount.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "状态长度超出");
			}
		}
		if (oldPropFeeAccount == null) {
			throw new AppBizException(CodeConst.BIS_CODE_13006, "实体不存在");
		}
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("accountID", propFeeAccount.getAccountID());
		param.put("money", propFeeAccount.getMoney());
		/***
		 * 新增账户余额
		 */
		if (propFeeAccount.getOperation().equals("1")) {
			PropFeeAmountLogDto propFeeAmountLog = new PropFeeAmountLogDto();
			propFeeAmountLog.setCreateuser(propFeeAccount.getCreateuserUser());
			propFeeAmountLog.setCreateuserID(propFeeAccount.getCreateuserID());
			propFeeAmountLog.setLogType(2);
			/**
			 * 操作(1加2,减)
			 */
			propFeeAmountLog.setOperation("1");
			propFeeAmountLog.setMoney(propFeeAccount.getMoney());
			propFeeAmountLog.setTotalMoney(oldPropFeeAccount.getMoney());
			/****
			 * 扣将
			 */
			propFeeAmountLog.setStatementType("13");
			if (StringUtils.isBlank(propFeeAccount.getRemark())) {
				propFeeAmountLog.setRemark("新增账户余额" + propFeeAccount.getMoney()
						+ "元");
			} else {
				propFeeAmountLog.setRemark(propFeeAccount.getRemark());
			}
			propFeeAccount.setMoney(propFeeAccount.getMoney()
					.add(oldPropFeeAccount.getMoney())
					.setScale(2, BigDecimal.ROUND_HALF_UP));
			propFeeAmountLog.setAccountID(propFeeAccount.getAccountID());

			if (!StringUtils.isBlank(propFeeAccount.getItem())) {
				propFeeAmountLog.setItem(propFeeAccount.getItem());
			}
			if (!StringUtils.isBlank(propFeeAccount.getStatementChannel())) {
				propFeeAmountLog.setStatementChannel(propFeeAccount
						.getStatementChannel());
			}
			if (!StringUtils.isBlank(propFeeAccount.getStatementType())) {
				propFeeAmountLog.setStatementType(propFeeAccount
						.getStatementType());
			}

			if (!StringUtils.isBlank(propFeeAccount.getLinkOrderID())) {
				propFeeAmountLog.setLinkID(propFeeAccount.getLinkOrderID());

			}
			int n = this.getSqlSession().update(
					"PropFeeAccountMapper.addAccount", param);
			if (n > 0) {
				propFeeAmountLogService.addPropFeeAmountLog(propFeeAmountLog);
			}
			if (n <= 0) {
				throw new AppBizException(100003, "余额账户操作失败");
			}
			return n;

		}
		/***
		 * 扣减账户余额
		 */
		if (propFeeAccount.getOperation().equals("2")) {
			PropFeeAmountLogDto propFeeAmountLog = new PropFeeAmountLogDto();
			propFeeAmountLog.setCreateuser(propFeeAccount.getCreateuserUser());
			propFeeAmountLog.setCreateuserID(propFeeAccount.getCreateuserID());
			propFeeAmountLog.setLogType(2);
			/**
			 * 操作(1加2,减)
			 */
			propFeeAmountLog.setOperation("2");
			propFeeAmountLog.setMoney(oldPropFeeAccount.getMoney().subtract(
					propFeeAccount.getMoney()));
			if (propFeeAmountLog.getMoney().compareTo(new BigDecimal(0)) < 0) {
				throw new AppBizException(CodeConst.SYS_CODE_1002,
						"亲，该业主房屋账户余额为负数，请处理后再进行缴费");
			}
			propFeeAmountLog.setMoney(propFeeAccount.getMoney());
			propFeeAmountLog.setAccountID(propFeeAccount.getAccountID());
			propFeeAmountLog.setTotalMoney(oldPropFeeAccount.getMoney());
			if (StringUtils.isBlank(propFeeAccount.getRemark())) {
				propFeeAmountLog.setRemark("减少账户余额" + propFeeAccount.getMoney()
						+ "元");
			} else {
				propFeeAmountLog.setRemark(propFeeAccount.getRemark());
			}
			/****
			 * 扣将
			 */
			propFeeAmountLog.setStatementType("14");
			propFeeAccount.setMoney(oldPropFeeAccount.getMoney().subtract(
					propFeeAccount.getMoney().setScale(2,
							BigDecimal.ROUND_HALF_UP)));

			if (!StringUtils.isBlank(propFeeAccount.getItem())) {
				propFeeAmountLog.setItem(propFeeAccount.getItem());
			}
			if (!StringUtils.isBlank(propFeeAccount.getStatementChannel())) {
				propFeeAmountLog.setStatementChannel(propFeeAccount
						.getStatementChannel());
			}
			if (!StringUtils.isBlank(propFeeAccount.getStatementType())) {
				propFeeAmountLog.setStatementType(propFeeAccount
						.getStatementType());
			}
			if (!StringUtils.isBlank(propFeeAccount.getLinkOrderID())) {
				propFeeAmountLog.setLinkID(propFeeAccount.getLinkOrderID());

			}

			int n = this.getSqlSession().update(
					"PropFeeAccountMapper.subAccount", param);
			if (n > 0) {
				propFeeAmountLogService.addPropFeeAmountLog(propFeeAmountLog);
			}
			if (n <= 0) {
				throw new AppBizException(100003, "余额账户操作失败");
			}
			return n;
			// propFeeAmountLogService.addPropFeeAmountLog(propFeeAmountLog);
		}
		// propFeeAccount.setTsedit(new Date());
		return 1;
	}

	@Override
	public PropFeeAccountDto findPropFeeAccountByID(String propFeeAccountID)
			throws AppBizException {
		if (StringUtils.isBlank(propFeeAccountID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeAccountDto oldPropFeeAccount = this.getSqlSession().selectOne(
				"PropFeeAccountSearchMapper.selectByPrimaryKey",
				propFeeAccountID);
		if (oldPropFeeAccount == null) {
			throw new AppBizException(CodeConst.BIS_CODE_13006, "余额账户不存在");
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeAccount.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_13006, "实体不存在");
		}
		return oldPropFeeAccount;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeAccountByLogic(String propFeeAccountID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeAccountID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeAccountDto oldPropFeeAccount = this
				.findPropFeeAccountByID(propFeeAccountID);

		oldPropFeeAccount.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeAccount.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeAccountMapper.updateByPrimaryKeySelective",
				oldPropFeeAccount);
	}

	@Override
	public PropFeeAccountDto findAccountByApartID(String apartmentID)
			throws AppBizException {

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeAccountDto oldPropFeeAccount = this.getSqlSession().selectOne(
				"PropFeeAccountSearchMapper.selectByApartID", apartmentID);

		return oldPropFeeAccount;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int handleAccount(AmountHandleDto amountHandleDto)
			throws AppBizException {
		if (amountHandleDto.getMoney() == null) {

			throw new AppBizException(100003, "操作金额不能为空");

		}

		Map<String, Object> param = new HashMap<String, Object>();
		PropFeeAccountDto p = this.findPropFeeAccountByID(amountHandleDto
				.getAccountID());
		if (p == null) {
			throw new AppBizException(100003, "账户不存在");

		}
		param.put("apartmentID", amountHandleDto.getApartmentID());
		param.put("accountID", amountHandleDto.getAccountID());
		param.put("money", amountHandleDto.getMoney());
		PropFeeAmountLogDto propFeeAmountLog = new PropFeeAmountLogDto();
		propFeeAmountLog.setCreateuser(amountHandleDto.getCreateuser());
		propFeeAmountLog.setCreateuserID(amountHandleDto.getCreateuserID());
		propFeeAmountLog.setOperation(amountHandleDto.getOperation());
		propFeeAmountLog.setMoney(amountHandleDto.getMoney());
		propFeeAmountLog.setTotalMoney(p.getMoney());
		propFeeAmountLog.setAccountID(amountHandleDto.getAccountID());
		propFeeAmountLog.setLinkID(amountHandleDto.getLinkID());
		if (!StringUtils.isBlank(amountHandleDto.getLogContent())) {
			propFeeAmountLog.setLogContent(amountHandleDto.getLogContent());
		}
		if (!StringUtils.isBlank(amountHandleDto.getStatementType())) {
			propFeeAmountLog.setStatementType(amountHandleDto
					.getStatementType());
		}
		if (!StringUtils.isBlank(amountHandleDto.getItem())) {
			propFeeAmountLog.setItem(amountHandleDto.getItem());
		}
		if (!StringUtils.isBlank(amountHandleDto.getStatementChannel())) {
			propFeeAmountLog.setStatementChannel(amountHandleDto
					.getStatementChannel());
		}

		/**
		 * 操作(1加2,减)
		 */

		if (!"1".equals(amountHandleDto.getOperation())
				&& !"2".equals(amountHandleDto.getOperation())) {

			throw new AppBizException(100003, "操作类型错误");
		}

		int n = 0;
		if ("2".equals(amountHandleDto.getOperation())) {

			propFeeAmountLog.setRemark("减少账户余额" + amountHandleDto.getMoney()
					+ "元");

			propFeeAmountLog.setLogType(2);
			n = this.getSqlSession().update("PropFeeAccountMapper.subAccount",
					param);
		}

		else if ("1".equals(amountHandleDto.getOperation())) {

			propFeeAmountLog.setRemark("增加账户余额" + amountHandleDto.getMoney()
					+ "元");

			propFeeAmountLog.setLogType(1);

			n = this.getSqlSession().update("PropFeeAccountMapper.addAccount",
					param);
		}

		if (amountHandleDto.getLogType() != null
				&& amountHandleDto.getLogType() != 0) {
			propFeeAmountLog.setLogType(amountHandleDto.getLogType());
		}

		if (!StringUtils.isBlank(amountHandleDto.getRemark())) {
			propFeeAmountLog.setRemark(amountHandleDto.getRemark());

		}

		if (n > 0) {

			propFeeAmountLogService.addPropFeeAmountLog(propFeeAmountLog);

		}

		if (n <= 0) {
			throw new AppBizException(100003, "余额账户操作失败");
		}
		return n;
	}

	@Override
	public List<Map<String, Object>> findPropFeeAccountByPage(String commID,
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("commID", commID);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAccountSearchMapper.findAllPropFeeAccount", map);
		return list;
	}

	@Override
	public List<PropFeeAccountDto> getPropFeeAccountByPage(String tsCreate,
			String search, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeAccountDto> list = this.getSqlSession().selectList(
				"PropFeeAccountSearchMapper.getAllPropFeeAccountDto", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeAccountByRelevantID(
			String accountID, String apartmentID, String status)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("accountID", accountID);
		map.put("apartmentID", apartmentID);
		map.put("status", status);

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeAccountSearchMapper.selectPropFeeAccountDtoyRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeAccountDto> findPropFeeAccountByRelevantID(
			String accountID, String apartmentID, String status,String accountType)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("accountID", accountID);
		map.put("apartmentID", apartmentID);
		map.put("status", status);
		map.put("accountTypeCode",accountType);

		List<PropFeeAccountDto> list = this.getSqlSession().selectList(
				"PropFeeAccountSearchMapper.findPropFeeAccountDtoByRelevantID",
				map);
		return list;
	}

	@Override
	public List<PropFeeAccountDto> bauthfindPropFeeAccount(
			List<String> propFeeAccountIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeAccountDto> list = this.getSqlSession().selectList(
				"PropFeeAccountSearchMapper.batchFindPropFeeAccount",
				propFeeAccountIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeAccountByMap(
			Map<String, Object> propFeeAccount) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAccountSearchMapper.batchFindPropFeeAccountByMap",
				propFeeAccount);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeAccount(List<PropFeeAccountDto> propFeeAccounts)
			throws AppBizException {

		if (propFeeAccounts == null || propFeeAccounts.size() <= 0) {
			return 0;
		}

		List<PropFeeAmountLogDto> propFeeAmountLogs = new ArrayList<PropFeeAmountLogDto>();
		for (PropFeeAccountDto propFeeAccount : propFeeAccounts) {

			PropFeeAmountLogDto propFeeAmountLog = new PropFeeAmountLogDto();
			propFeeAmountLog.setCreateuser(propFeeAccount.getCreateuserUser());
			propFeeAmountLog.setCreateuserID(propFeeAccount.getCreateuserID());
			propFeeAmountLog.setLogType(1);
			/**
			 * 操作(1加2,减)
			 */
			propFeeAmountLog.setOperation("1");
			propFeeAmountLog.setMoney(propFeeAccount.getMoney());
			propFeeAmountLog.setTotalMoney(new BigDecimal(0));
			propFeeAmountLog.setRemark("导入账户余额" + propFeeAccount.getMoney()
					+ "元");
			propFeeAmountLog.setLogID(IdUtil.getuuId());
			propFeeAmountLog.setItemStat(TypeConst.FLAG_ON);
			propFeeAmountLog.setAccountID(propFeeAccount.getAccountID());
			propFeeAmountLog.setTscreate(new Date());
			propFeeAmountLog.setTimes(String.valueOf(propFeeAmountLog
					.getTscreate().getTime()));
			propFeeAmountLogs.add(propFeeAmountLog);
		}
		if (propFeeAmountLogs != null && propFeeAmountLogs.size() > 0) {
			this.getSqlSession().insert(
					"PropFeeAmountLogBauthMapper.bauthInsert",
					propFeeAmountLogs);
		}
		return this.getSqlSession().insert(
				"PropFeeAccountBauthMapper.bauthInsert", propFeeAccounts);
	}

	@Override
	public List<Map<String, Object>> getApaptMentInfoByName(
			Map<String, Object> houseInfo) throws AppBizException {
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAccountSearchMapper.getApaptMentInfoByName", houseInfo);
		return list;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editFeeAccount(PropFeeAccountDto propFeeAccount)
			throws AppBizException {

		Assert.notNull(propFeeAccount);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeAccount.getAccountID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeAccountDto oldPropFeeAccount = this
				.findPropFeeAccountByID(propFeeAccount.getAccountID());

		if (StringUtils.isBlank(propFeeAccount.getOperation())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "亲，操作类型");
		}

		if (!propFeeAccount.getOperation().equals("1")
				&& !propFeeAccount.getOperation().equals("2")) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "亲，操作类型错误");
		}

		if (!StringUtils.isBlank(propFeeAccount.getStatus())) {
			if (propFeeAccount.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "状态长度超出");
			}
		}
		if (oldPropFeeAccount == null) {
			throw new AppBizException(CodeConst.BIS_CODE_13006, "实体不存在");
		}

		/***
		 * 新增账户余额
		 */
		if (propFeeAccount.getOperation().equals("1")) {
			PropFeeAmountLogDto propFeeAmountLog = new PropFeeAmountLogDto();
			propFeeAmountLog.setCreateuser(propFeeAccount.getCreateuserUser());
			propFeeAmountLog.setCreateuserID(propFeeAccount.getCreateuserID());

			if (StringUtils.isBlank(propFeeAccount.getLgType())) {

				propFeeAmountLog.setLogType(2);
			} else {
				propFeeAmountLog.setLogType(Integer.parseInt(propFeeAccount
						.getLgType()));
			}
			/**
			 * 操作(1加2,减)
			 */
			propFeeAmountLog.setOperation("1");
			propFeeAmountLog.setMoney(propFeeAccount.getMoney());
			propFeeAmountLog.setTotalMoney(oldPropFeeAccount.getMoney());

			if (StringUtils.isBlank(propFeeAccount.getRemark())) {
				propFeeAmountLog.setRemark("新增账户余额" + propFeeAccount.getMoney()
						+ "元");
			} else {
				propFeeAmountLog.setRemark(propFeeAccount.getRemark());
			}
			propFeeAccount.setMoney(propFeeAccount.getMoney()
					.add(oldPropFeeAccount.getMoney())
					.setScale(2, BigDecimal.ROUND_HALF_UP));
			propFeeAmountLog.setAccountID(propFeeAccount.getAccountID());
			propFeeAmountLogService.addPropFeeAmountLog(propFeeAmountLog);
		}
		/***
		 * 扣减账户余额
		 */
		if (propFeeAccount.getOperation().equals("2")) {
			PropFeeAmountLogDto propFeeAmountLog = new PropFeeAmountLogDto();
			propFeeAmountLog.setCreateuser(propFeeAccount.getCreateuserUser());
			propFeeAmountLog.setCreateuserID(propFeeAccount.getCreateuserID());
			if (StringUtils.isBlank(propFeeAccount.getLgType())) {
				propFeeAmountLog.setLogType(2);
			} else {
				propFeeAmountLog.setLogType(Integer.parseInt(propFeeAccount
						.getLgType()));
			}
			/**
			 * 操作(1加2,减)
			 */
			propFeeAmountLog.setOperation("2");
			propFeeAmountLog.setMoney(oldPropFeeAccount.getMoney().subtract(
					propFeeAccount.getMoney()));
			propFeeAmountLog.setMoney(propFeeAccount.getMoney());
			propFeeAmountLog.setAccountID(propFeeAccount.getAccountID());
			propFeeAmountLog.setTotalMoney(oldPropFeeAccount.getMoney());
			if (StringUtils.isBlank(propFeeAccount.getRemark())) {
				propFeeAmountLog.setRemark("减少账户余额" + propFeeAccount.getMoney()
						+ "元");
			} else {
				propFeeAmountLog.setRemark(propFeeAccount.getRemark());
			}
			propFeeAccount.setMoney(oldPropFeeAccount.getMoney().subtract(
					propFeeAccount.getMoney().setScale(2,
							BigDecimal.ROUND_HALF_UP)));
			propFeeAmountLogService.addPropFeeAmountLog(propFeeAmountLog);
		}
		propFeeAccount.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeAccountMapper.updateByPrimaryKeySelective",
				propFeeAccount);
	}

	@Override
	public PropFeeAccountDto getAccountByApartID(String apartmentID)
			throws AppBizException {
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeAccountDto oldPropFeeAccount = this.getSqlSession().selectOne(
				"PropFeeAccountSearchMapper.selectByApartID", apartmentID);

		if (oldPropFeeAccount == null) {
			oldPropFeeAccount = new PropFeeAccountDto();
			oldPropFeeAccount.setApartmentID(apartmentID);
			oldPropFeeAccount.setMoney(BigDecimal.ZERO);
			oldPropFeeAccount.setStatus("1");
			oldPropFeeAccount.setAccountID(IdUtil.getuuId());
			int n = this.addPropFeeAccount(oldPropFeeAccount);
		}

		return oldPropFeeAccount;
	}

	@Override
	public List<Map<String, Object>> getAllPropFeeAccountInfo(String commID,
			String apartmentType, String searchType, String search,
			String startTime, int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isBlank(startTime)) {
			map.put("startTime", Long.parseLong(startTime) / 1000);
		}
		map.put("apartmentType", apartmentType);
		map.put("commID", commID);
		if (!StringUtils.isBlank(searchType)) {
			map.put("searchType", String.valueOf(searchType));
		}
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAccountSearchMapper.findAccountInfoByMonth", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> getAllPropFeeAccountInfo(String commID,
			String apartmentType, String searchType, String search,
			String startTime) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isBlank(startTime)) {
			map.put("startTime", Long.parseLong(startTime) / 1000);
		}
		map.put("apartmentType", apartmentType);
		map.put("commID", commID);
		if (!StringUtils.isBlank(searchType)) {
			map.put("searchType", String.valueOf(searchType));
		}
		map.put("search", search);

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAccountSearchMapper.findAccountInfoByMonth", map);
		return list;
	}

	@Override
	public Map<String, Object> getSumAccountInfo(String commID,
			String apartmentType, String searchType, String search,
			String startTime) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isBlank(startTime)) {
			map.put("startTime", Long.parseLong(startTime) / 1000);
		}
		map.put("apartmentType", apartmentType);
		map.put("commID", commID);
		if (!StringUtils.isBlank(searchType)) {
			map.put("searchType", String.valueOf(searchType));
		}
		map.put("search", search);

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAccountSearchMapper.findAccountInfoByMonth", map);
		list = PropertyUtils.turnToEntity(list);
		BigDecimal startMoonMoney = new BigDecimal("0");
		BigDecimal endMoonMoney = new BigDecimal("0");

		BigDecimal preMoonMoney = new BigDecimal("0");
		BigDecimal offMoonMoney = new BigDecimal("0");
		for (Map<String, Object> account : list) {
			if (account.get("startMoonMoney") != null) {
				startMoonMoney = startMoonMoney.add(new BigDecimal(account.get(
						"startMoonMoney").toString()));
			}
			if (account.get("endMoonMoney") != null) {
				endMoonMoney = endMoonMoney.add(new BigDecimal(account.get(
						"endMoonMoney").toString()));
			}
			if (account.get("preMoonMoney") != null) {
				preMoonMoney = preMoonMoney.add(new BigDecimal(account.get(
						"preMoonMoney").toString()));
			}
			if (account.get("offMoonMoney") != null) {
				offMoonMoney = offMoonMoney.add(new BigDecimal(account.get(
						"offMoonMoney").toString()));
			}
		}
		resultMap.put("startMoonMoney", startMoonMoney);
		resultMap.put("endMoonMoney", endMoonMoney);
		resultMap.put("preMoonMoney", preMoonMoney);
		resultMap.put("offMoonMoney", offMoonMoney);

		return resultMap;
	}

	@Override
	public List<Map<String, Object>> getAountInfoHasMoney(String commID)
			throws AppBizException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("commID", commID);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeAccountSearchMapper.getAountInfoHasMoney", map);
		return list;

	}

	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	@Override
	public PropFeeAccountDto addPropFeeLog(PropFeeAccountDto propFeeAccount,
			PropFeeAccountDto oldPropFeeAccount) throws AppBizException {

		Assert.notNull(propFeeAccount);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeAccount.getAccountID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		if (StringUtils.isBlank(propFeeAccount.getOperation())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "亲，操作类型");
		}

		if (!propFeeAccount.getOperation().equals("1")
				&& !propFeeAccount.getOperation().equals("2")) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "亲，操作类型错误");
		}

		if (!StringUtils.isBlank(propFeeAccount.getStatus())) {
			if (propFeeAccount.getStatus().length() > 1) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "状态长度超出");
			}
		}
		if (oldPropFeeAccount == null) {
			// throw new AppBizException(CodeConst.BIS_CODE_13006, "实体不存在");
			oldPropFeeAccount = new PropFeeAccountDto();
			oldPropFeeAccount.setMoney(new BigDecimal("0"));
		}

		/***
		 * 新增账户余额
		 */
		if (propFeeAccount.getOperation().equals("1")) {
			PropFeeAmountLogDto propFeeAmountLog = new PropFeeAmountLogDto();
			propFeeAmountLog.setCreateuser(propFeeAccount.getCreateuserUser());
			propFeeAmountLog.setCreateuserID(propFeeAccount.getCreateuserID());
			propFeeAmountLog.setLogType(2);
			/**
			 * 操作(1加2,减)
			 */
			propFeeAmountLog.setOperation("1");
			propFeeAmountLog.setMoney(propFeeAccount.getMoney());
			propFeeAmountLog.setTotalMoney(oldPropFeeAccount.getMoney());
			/****
			 * 扣将
			 */
			propFeeAmountLog.setStatementType("13");
			if (StringUtils.isBlank(propFeeAccount.getRemark())) {
				propFeeAmountLog.setRemark("新增账户余额" + propFeeAccount.getMoney()
						+ "元");
			} else {
				propFeeAmountLog.setRemark(propFeeAccount.getRemark());
			}
			propFeeAccount.setMoney(propFeeAccount.getMoney()
					.add(oldPropFeeAccount.getMoney())
					.setScale(2, BigDecimal.ROUND_HALF_UP));
			propFeeAmountLog.setAccountID(propFeeAccount.getAccountID());

			if (!StringUtils.isBlank(propFeeAccount.getItem())) {
				propFeeAmountLog.setItem(propFeeAccount.getItem());
			}
			if (!StringUtils.isBlank(propFeeAccount.getStatementChannel())) {
				propFeeAmountLog.setStatementChannel(propFeeAccount
						.getStatementChannel());
			}
			if (!StringUtils.isBlank(propFeeAccount.getStatementType())) {
				propFeeAmountLog.setStatementType(propFeeAccount
						.getStatementType());
			}

			if (!StringUtils.isBlank(propFeeAccount.getLinkOrderID())) {
				propFeeAmountLog.setLinkID(propFeeAccount.getLinkOrderID());

			}

			propFeeAmountLogService.addPropFeeAmountLog(propFeeAmountLog);
			return propFeeAccount;

		}
		/***
		 * 扣减账户余额
		 */
		if (propFeeAccount.getOperation().equals("2")) {
			PropFeeAmountLogDto propFeeAmountLog = new PropFeeAmountLogDto();
			propFeeAmountLog.setCreateuser(propFeeAccount.getCreateuserUser());
			propFeeAmountLog.setCreateuserID(propFeeAccount.getCreateuserID());
			propFeeAmountLog.setLogType(2);
			/**
			 * 操作(1加2,减)
			 */
			propFeeAmountLog.setOperation("2");
			propFeeAmountLog.setMoney(oldPropFeeAccount.getMoney().subtract(
					propFeeAccount.getMoney()));

			propFeeAmountLog.setMoney(propFeeAccount.getMoney());
			propFeeAmountLog.setAccountID(propFeeAccount.getAccountID());
			propFeeAmountLog.setTotalMoney(oldPropFeeAccount.getMoney());
			if (StringUtils.isBlank(propFeeAccount.getRemark())) {
				propFeeAmountLog.setRemark("减少账户余额" + propFeeAccount.getMoney()
						+ "元");
			} else {
				propFeeAmountLog.setRemark(propFeeAccount.getRemark());
			}
			/****
			 * 扣将
			 */
			propFeeAmountLog.setStatementType("14");
			propFeeAccount.setMoney(oldPropFeeAccount.getMoney().subtract(
					propFeeAccount.getMoney().setScale(2,
							BigDecimal.ROUND_HALF_UP)));

			if (!StringUtils.isBlank(propFeeAccount.getItem())) {
				propFeeAmountLog.setItem(propFeeAccount.getItem());
			}
			if (!StringUtils.isBlank(propFeeAccount.getStatementChannel())) {
				propFeeAmountLog.setStatementChannel(propFeeAccount
						.getStatementChannel());
			}
			if (!StringUtils.isBlank(propFeeAccount.getStatementType())) {
				propFeeAmountLog.setStatementType(propFeeAccount
						.getStatementType());
			}
			if (!StringUtils.isBlank(propFeeAccount.getLinkOrderID())) {
				propFeeAmountLog.setLinkID(propFeeAccount.getLinkOrderID());

			}
			propFeeAmountLogService.addPropFeeAmountLog(propFeeAmountLog);
			return propFeeAccount;
		}

		return propFeeAccount;

	}

	@Override
	public PropFeeAccountDto handleAccountLog(AmountHandleDto amountHandleDto,
			PropFeeAccountDto p) throws AppBizException {
		if (amountHandleDto.getMoney() == null) {

			throw new AppBizException(100003, "操作金额不能为空");

		}

		PropFeeAmountLogDto propFeeAmountLog = new PropFeeAmountLogDto();
		propFeeAmountLog.setCreateuser(amountHandleDto.getCreateuser());
		propFeeAmountLog.setCreateuserID(amountHandleDto.getCreateuserID());
		propFeeAmountLog.setOperation(amountHandleDto.getOperation());
		propFeeAmountLog.setMoney(amountHandleDto.getMoney());
		propFeeAmountLog.setTotalMoney(p.getMoney());
		propFeeAmountLog.setAccountID(amountHandleDto.getAccountID());
		propFeeAmountLog.setLinkID(amountHandleDto.getLinkID());
		if (!StringUtils.isBlank(amountHandleDto.getLogContent())) {
			propFeeAmountLog.setLogContent(amountHandleDto.getLogContent());
		}
		if (!StringUtils.isBlank(amountHandleDto.getStatementType())) {
			propFeeAmountLog.setStatementType(amountHandleDto
					.getStatementType());
		}
		if (!StringUtils.isBlank(amountHandleDto.getItem())) {
			propFeeAmountLog.setItem(amountHandleDto.getItem());
		}
		if (!StringUtils.isBlank(amountHandleDto.getStatementChannel())) {
			propFeeAmountLog.setStatementChannel(amountHandleDto
					.getStatementChannel());
		}
		/**
		 * 操作(1加2,减)
		 */

		if (!"1".equals(amountHandleDto.getOperation())
				&& !"2".equals(amountHandleDto.getOperation())) {

			throw new AppBizException(100003, "操作类型错误");
		}

		int n = 1;
		if ("2".equals(amountHandleDto.getOperation())) {

			propFeeAmountLog.setRemark("减少账户余额" + amountHandleDto.getMoney()
					+ "元");

			propFeeAmountLog.setLogType(2);

		} else if ("1".equals(amountHandleDto.getOperation())) {
			propFeeAmountLog.setRemark("增加账户余额" + amountHandleDto.getMoney()
					+ "元");
			propFeeAmountLog.setLogType(1);

		}

		if (amountHandleDto.getLogType() != null
				&& amountHandleDto.getLogType() != 0) {
			propFeeAmountLog.setLogType(amountHandleDto.getLogType());
		}

		if (!StringUtils.isBlank(amountHandleDto.getRemark())) {
			propFeeAmountLog.setRemark(amountHandleDto.getRemark());

		}

		if (n > 0) {

			propFeeAmountLogService.addPropFeeAmountLog(propFeeAmountLog);

		}

		return p;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int handleAccountInfo(AmountHandleDto amountHandleDto)
			throws AppBizException {
		if (amountHandleDto.getMoney() == null) {

			throw new AppBizException(100003, "操作金额不能为空");

		}

		Map<String, Object> param = new HashMap<String, Object>();
		PropFeeAccountDto p = this.findPropFeeAccountByID(amountHandleDto
				.getAccountID());
		if (p == null) {
			throw new AppBizException(100003, "账户不存在");

		}
		param.put("apartmentID", amountHandleDto.getApartmentID());
		param.put("accountID", amountHandleDto.getAccountID());
		param.put("money", amountHandleDto.getMoney());
		PropFeeAmountLogDto propFeeAmountLog = new PropFeeAmountLogDto();
		propFeeAmountLog.setCreateuser(amountHandleDto.getCreateuser());
		propFeeAmountLog.setCreateuserID(amountHandleDto.getCreateuserID());
		propFeeAmountLog.setOperation(amountHandleDto.getOperation());
		propFeeAmountLog.setMoney(amountHandleDto.getMoney());
		propFeeAmountLog.setTotalMoney(p.getMoney());
		propFeeAmountLog.setAccountID(amountHandleDto.getAccountID());
		propFeeAmountLog.setLinkID(amountHandleDto.getLinkID());
		if (!StringUtils.isBlank(amountHandleDto.getLogContent())) {
			propFeeAmountLog.setLogContent(amountHandleDto.getLogContent());
		}
		if (!StringUtils.isBlank(amountHandleDto.getStatementType())) {
			propFeeAmountLog.setStatementType(amountHandleDto
					.getStatementType());
		}
		if (!StringUtils.isBlank(amountHandleDto.getItem())) {
			propFeeAmountLog.setItem(amountHandleDto.getItem());
		}
		if (!StringUtils.isBlank(amountHandleDto.getStatementChannel())) {
			propFeeAmountLog.setStatementChannel(amountHandleDto
					.getStatementChannel());
		}

		/**
		 * 操作(1加2,减)
		 */

		if (!"1".equals(amountHandleDto.getOperation())
				&& !"2".equals(amountHandleDto.getOperation())) {
			throw new AppBizException(100003, "操作类型错误");
		}
		int n = 0;
		if ("2".equals(amountHandleDto.getOperation())) {
			propFeeAmountLog.setRemark("减少账户余额" + amountHandleDto.getMoney()
					+ "元");
			propFeeAmountLog.setLogType(2);
			n = this.getSqlSession().update(
					"PropFeeAccountMapper.subAccountInfo", param);
		}
		else if ("1".equals(amountHandleDto.getOperation())) {
			propFeeAmountLog.setRemark("增加账户余额" + amountHandleDto.getMoney()
					+ "元");
			propFeeAmountLog.setLogType(1);

			n = this.getSqlSession().update("PropFeeAccountMapper.addAccount",
					param);
		}
		if (amountHandleDto.getLogType() != null
				&& amountHandleDto.getLogType() != 0) {
			propFeeAmountLog.setLogType(amountHandleDto.getLogType());
		}
		if (!StringUtils.isBlank(amountHandleDto.getRemark())) {
			propFeeAmountLog.setRemark(amountHandleDto.getRemark());

		}
		if (n > 0) {
			propFeeAmountLogService.addPropFeeAmountLog(propFeeAmountLog);
		}
		if (n <= 0) {
			throw new AppBizException(100003, "余额账户操作失败");
		}
		return n;
	}
}
